
package com.craterhill.java.ems.web;

import java.io.File;
import java.io.IOException;

import javax.servlet.ServletContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.craterhill.java.ems.common.beans.School;
import com.craterhill.java.ems.common.utils.Constants;
import com.craterhill.java.ems.service.CountryService;
import com.craterhill.java.ems.service.DateFormatService;
import com.craterhill.java.ems.service.SchoolLevelService;
import com.craterhill.java.ems.service.SchoolService;
import com.craterhill.java.ems.service.SchoolTypeService;
import com.craterhill.java.ems.service.StateService;

@Controller
public class SchoolManagementController {

	private final SchoolTypeService schoolTypeService;
	private final SchoolLevelService schoolLevelService;
	private final SchoolService schoolService;
	private final CountryService countryService;
	private final StateService stateService;
	private final DateFormatService dateFormatService;

	private final String FILE_TO_SAVE;

	@Autowired
	public SchoolManagementController(final SchoolTypeService schoolTypeService, final SchoolLevelService schoolLevelService,
			final SchoolService schoolService, final CountryService countryService, final StateService stateService,
			final DateFormatService dateFormatService, final ServletContext servletContext) {
		this.schoolTypeService = schoolTypeService;
		this.schoolLevelService = schoolLevelService;
		this.schoolService = schoolService;
		this.countryService = countryService;
		this.stateService = stateService;
		this.dateFormatService = dateFormatService;

		this.FILE_TO_SAVE = servletContext.getRealPath("/") + Constants.APP_IMAGE_UPLOAD_PATH;
		File appImageToSave = new File(FILE_TO_SAVE);
		if(!appImageToSave.exists()){
			appImageToSave.mkdirs();
		}
	}

	@RequestMapping(value = "/setupSchoolManagementForm.ems", method = RequestMethod.GET)
	public String setupForm(final @RequestParam(required = false, defaultValue = "0") int schoolId, final ModelMap modelMap) {
		modelMap.addAttribute("schoolList", schoolService.getSchools());
		modelMap.addAttribute("schoolTypeList", schoolTypeService.getSchoolTypes());
		modelMap.addAttribute("schoolLevelList", schoolLevelService.getSchoolLevels());
		modelMap.addAttribute("dateFormatList", dateFormatService.getDateFormats());
		modelMap.addAttribute("countryList", countryService.getCountrys());
		if(schoolId != 0){
			School school = schoolService.getSchool(schoolId);
			modelMap.addAttribute("school", school);
			modelMap.addAttribute("stateList", stateService.getStates(school.getCountryId()));
		}
		return "manage_school";
	}

	@RequestMapping(value = "/saveSchool.ems", method = RequestMethod.POST)
	public String savePosition(final @ModelAttribute School school) {

		String returnUrl = null;

		try{

			CommonsMultipartFile commonsMultipartFile = null;
			File imageToSave = null;

			{
				commonsMultipartFile = school.getLogoImageMulitpartFile();
				imageToSave = new File(this.FILE_TO_SAVE + commonsMultipartFile.getOriginalFilename());
				commonsMultipartFile.transferTo(imageToSave);
				school.setLogoImagePath(imageToSave.getAbsolutePath());
			}

			{
				commonsMultipartFile = school.getBannerImageMultipartFile();
				imageToSave = new File(this.FILE_TO_SAVE + commonsMultipartFile.getOriginalFilename());
				commonsMultipartFile.transferTo(imageToSave);
				school.setBannerImagePath(imageToSave.getAbsolutePath());
			}

			returnUrl = "redirect:setupSchoolManagementForm.ems";

			if(school.getSchoolId() == 0){
				schoolService.saveSchool(school);
			} else{
				schoolService.updateSchool(school);
				returnUrl += "?schoolId=" + school.getSchoolId();
			}

		} catch(IllegalStateException e){
			e.printStackTrace();
		} catch(IOException e){
			e.printStackTrace();
		}
		return returnUrl;
	}

	@RequestMapping(value = "/deleteSchool.ems", method = RequestMethod.GET)
	public String deletePosition(final @RequestParam int schoolId) {
		schoolService.deleteSchool(schoolId);
		return "redirect:setupSchoolManagementForm.ems";
	}

	@ModelAttribute
	public School getSchool() {
		return new School();
	}
}
